package com.baikeyang.service.dictionaries.impl;

import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.Dictionaries;
import com.baikeyang.common.util.shiro.ShiroUtil;
import com.baikeyang.mapper.DictionariesMapper;
import com.baikeyang.service.dictionaries.IDictionariesService;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BaiKeyang
 * @since 2019-05-12
 */
@Service(value = "dictionariesService")
public class DictionariesServiceImpl extends SuperServiceImpl<DictionariesMapper, Dictionaries> implements IDictionariesService {

	@Autowired
	private DictionariesMapper dictionariesMapper;

	@Override
	public List<Dictionaries> queryDictionariesByCode(String code) {
		//String code = "activity_type";
		return dictionariesMapper.selectDictionariesByCode(code);
	}

	@Override
	public IPage<Dictionaries> queryDictionariesPageByCode(Page<Dictionaries> pageParam, String code) {
		return dictionariesMapper.selectDictionariesPageByCode(pageParam, code);
	}

	@Override
	public Dictionaries queryDictionaryByCode(String code) {
		QueryWrapper<Dictionaries> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("code", code);
		return dictionariesMapper.selectOne(queryWrapper);
	}

	@Override
	public Dictionaries queryDictionaryById(Integer id) {
		return dictionariesMapper.selectById(id);
	}

	@Override
	public ResponseResult saveDictionary(Dictionaries dictionaries) {
		dictionaries.setCreateTime(new Date());
		dictionaries.setCreateUser(ShiroUtil.getCurrentUserId());
		dictionaries.setUpdateTime(new Date());
		dictionaries.setUpdateUser(ShiroUtil.getCurrentUserId());
		Integer flag = dictionariesMapper.insert(dictionaries);
		if (flag > 0) {
			return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
		}
		return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
	}

	@Override
	public ResponseResult updateDictionary(Dictionaries dictionaries) {
		dictionaries.setUpdateTime(new Date());
		dictionaries.setUpdateUser(ShiroUtil.getCurrentUserId());
		Integer flag = dictionariesMapper.updateById(dictionaries);
		if (flag > 0) {
			return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
		}
		return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
	}

	@Override
	public Integer queryMaxValueByCode(String code) {
		Integer value = dictionariesMapper.selectMaxValueByCode(code);
		return value == null ? 0 : value;
	}

	@Override
	public int deleteDictionary(int id) {
		return dictionariesMapper.deleteById(id);
	}
}
